package com.jlnk.androidfmc.fragment.fragments;

import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.jlnk.androidfmc.MainActivity;
import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.fragment.ConsultFragment;
import com.jlnk.androidfmc.fragment.HotNewFragment;
import com.jlnk.androidfmc.fragment.ZnbNewFragment;
import com.jlnk.androidfmc.view.NewsPost;

public class SchoolFragment extends Fragment {

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private FrameLayout frameLayout;
    private HotNewFragment hotNewFragment;
    private static final int NEWS_POST_REQUEST_CODE = 1;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;



    public SchoolFragment() {
        // Required empty public constructor
    }

    public static SchoolFragment newInstance(String param1, String param2) {
        SchoolFragment fragment = new SchoolFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // 使用SecondActivity的布局
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_school, container, false);

        // 初始化视图
        fragmentManager = getChildFragmentManager();
        frameLayout = root.findViewById(R.id.fragment_container);

        // 初始化按钮和Fragment
        initializeButtonsAndFragments(root);

        // 设置返回按钮的点击事件

        return root;
    }

    private void initializeButtonsAndFragments(ViewGroup root) {

        Button buttonHotNews = root.findViewById(R.id.button_hot_news);
        Button buttonExpertAdvice = root.findViewById(R.id.button_expert_advice);
        Button buttonSupportNews = root.findViewById(R.id.button_support_news);

        buttonHotNews.setOnClickListener(view -> onNewClick(view));
        buttonExpertAdvice.setOnClickListener(view -> onExpertAdviceClick(view));
        buttonSupportNews.setOnClickListener(view -> onSupportNewsClick(view));

        hotNewFragment = new HotNewFragment(); // 初始化hotNewFragment
        loadFragment(hotNewFragment); // 加载初始Fragment
    }


    private void loadFragment(Fragment fragment) {
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }


    public void onNewClick(View view) {
        loadFragment(new HotNewFragment());
    }
    public void onExpertAdviceClick(View view) {
        loadFragment(new ConsultFragment());
    }

    public void onSupportNewsClick(View view) {
        loadFragment(new ZnbNewFragment());
    }
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 通知MainActivity在非首页隐藏"去卖货"按钮和图片
        MainActivity mainActivity = (MainActivity) getActivity();
        if (mainActivity != null) {
            mainActivity.setGoSellVisibility(false);
        }
    }

}